
  {FINAL.PA2}

  {Write Final Version of Game}
  PROCEDURE WriteFinalVersion;
  VAR
    i : Integer;
    FreeBytes, EstimatedSize : Real;
    st : words;
    Write_Text_file : Text;
    RoomFile : FILE OF rooms;
    NounFile : FILE OF nouns;
    CreatureFile : FILE OF creatures;
    CommandFile : FILE OF Command;

    FUNCTION Free : Real;
      {Returns free space on the default disk}
    VAR
      reg : Registers;

    BEGIN                         {Free}
      WITH reg DO
        BEGIN
          ah := $36;              {DOS function number}
          dl := $00;              {drive number: default=00;a=01,b=02,c=03,etc}
          MSDOS(reg);             {call DOS}
          Free := 1.0*ax*bx*cx;   {multiple by 1.0 to create a REAL value}
        END;
    END;                          {Free}

  BEGIN                           {WriteFinalVersion}
    EstimatedSize := 11000.0+170.0*MaxRoom;
    IF Last_CMD > 0 THEN EstimatedSize := EstimatedSize+140.0*Last_CMD;
    IF MaxNoun >= First_noun THEN
      EstimatedSize := EstimatedSize+279.0*(MaxNoun-First_noun+1.0);
    IF MaxCreature >= First_creature THEN
      EstimatedSize := EstimatedSize+380.0*(MaxCreature-First_creature+1.0);
    EstimatedSize := (EstimatedSize+500.0)/1000.0;

    FreeBytes := (Free+500.0)/1000.0; {free space in K on default drive}
    CLRSCR;
    WriteLn('About to begin writing Final Version Data Files.');
    WriteLn;
    WriteLn('Data files require approximately ', EstimatedSize:5:0, ' K bytes of disk space.');
    WriteLn('There are currently ', FreeBytes:5:0, ' K bytes free on your disk.');
    WriteLn;
    IF (EstimatedSize > (0.9*FreeBytes)) THEN
      BEGIN
        WriteLn('You may wish to put in a fresh disk with lots of room.');
        WriteLn;
      END;
    Write('Hit RETURN when ready to proceed ');
    ReadLn(st);
    CLRSCR;
    WriteLn('Final version of the game disk MUST contain following files:');
    WriteLn;
  {$IFDEF NormalVersion}
    WriteLn(' RUN.EXE');
  {$ELSE}
    WriteLn(' RUNB.EXE  <-- The BIG version of RUN');
  {$ENDIF}
    Capitalize(Adventure_Name);
    WriteLn(' ', Adventure_Name+'.BAT');
    WriteLn(' ', Adventure_Name+'.D$$');
    WriteLn(' ', Adventure_Name+'.DA1');

    {first write the text file with general data}
    Assign(Write_Text_file, Adventure_Name+'.DA1');
    Rewrite(Write_Text_file);
    WriteLn(Write_Text_file, Starting_room);
    WriteLn(Write_Text_file, Treasure_Room);
    WriteLn(Write_Text_file, Resurrection_Room);
    WriteLn(Write_Text_file, Max_Lives);
    WriteLn(Write_Text_file, Maximum_Score);
    WriteLn(Write_Text_file, MaxRoom);
    WriteLn(Write_Text_file, MaxNoun);
    WriteLn(Write_Text_file, MaxCreature);
    WriteLn(Write_Text_file, Last_CMD);
    WriteLn(Write_Text_file, Items_Being_Carried);
    WriteLn(Write_Text_file, Items_Being_Worn);
    WriteLn(Write_Text_file, Intro_Ptr.start);
    WriteLn(Write_Text_file, Intro_Ptr.len);

    FOR i := 1 TO MaxQuestion DO
      BEGIN
        WriteLn(Write_Text_file, Question[i]);
        WriteLn(Write_Text_file, Answer[i]);
      END;

    IF Any_Special_Cmds
    THEN
      BEGIN
        WriteLn(Write_Text_file, 'TRUE');
        FOR i := 0 TO Last_Creature DO
          BEGIN
            WriteLn(Write_Text_file, StartingIndex[i]);
            WriteLn(Write_Text_file, EndingIndex[i]);
          END;
      END                         {Any_Speical_Cmds}
    ELSE WriteLn(Write_Text_file, 'FALSE');

    FOR i := 0 TO Num_Verbs DO WriteLn(Write_Text_file, SYN[i]); {verb synonyms}

    FOR i := First_Message TO Last_Message DO
      BEGIN                       {write pointers to description file}
        WriteLn(Write_Text_file, Message_Ptr[i].start);
        WriteLn(Write_Text_file, Message_Ptr[i].len);
      END;

    FOR i := First_Room TO MaxRoom DO
      BEGIN
        WriteLn(Write_Text_file, Room_Ptr[i].start);
        WriteLn(Write_Text_file, Room_Ptr[i].len);
        WriteLn(Write_Text_file, Help_Ptr[i].start);
        WriteLn(Write_Text_file, Help_Ptr[i].len);
        WriteLn(Write_Text_file, Special_Ptr[i].start);
        WriteLn(Write_Text_file, Special_Ptr[i].len);
      END;

    FOR i := First_noun TO MaxNoun DO
      BEGIN
        WriteLn(Write_Text_file, Noun_Ptr[i].start);
        WriteLn(Write_Text_file, Noun_Ptr[i].len);
        WriteLn(Write_Text_file, Push_Ptr[i].start);
        WriteLn(Write_Text_file, Push_Ptr[i].len);
        WriteLn(Write_Text_file, Pull_Ptr[i].start);
        WriteLn(Write_Text_file, Pull_Ptr[i].len);
        WriteLn(Write_Text_file, Text_Ptr[i].start);
        WriteLn(Write_Text_file, Text_Ptr[i].len);
        WriteLn(Write_Text_file, Turn_Ptr[i].start);
        WriteLn(Write_Text_file, Turn_Ptr[i].len);
        WriteLn(Write_Text_file, Play_Ptr[i].start);
        WriteLn(Write_Text_file, Play_Ptr[i].len);
      END;

    IF MaxCreature > 0 THEN FOR i := First_creature TO MaxCreature DO
      BEGIN
        WriteLn(Write_Text_file, Creature_Ptr[i].start);
        WriteLn(Write_Text_file, Creature_Ptr[i].len);
      END;

    Close(Write_Text_file);

    {next write the ROOM file}
    WriteLn(' ', Adventure_Name+'.DA2');
    Assign(RoomFile, Adventure_Name+'.DA2');
    Rewrite(RoomFile);
    FOR i := First_Room TO MaxRoom DO Write(RoomFile, Room[i]^);
    Close(RoomFile);

    {next write the NOUN file}
    WriteLn(' ', Adventure_Name+'.DA3');
    Assign(NounFile, Adventure_Name+'.DA3');
    Rewrite(NounFile);
    FOR i := First_noun TO MaxNoun DO Write(NounFile, N[i]^);
    Close(NounFile);

    {next write the CREATURE file}
    IF MaxCreature > 0 THEN
      BEGIN
        WriteLn(' ', Adventure_Name+'.DA4');
        Assign(CreatureFile, Adventure_Name+'.DA4');
        Rewrite(CreatureFile);
        FOR i := First_creature TO MaxCreature DO Write(CreatureFile, M[i]^);
        Close(CreatureFile);
      END;

    {next write the Special Command file -- if used}
    IF Any_Special_Cmds THEN
      BEGIN
        WriteLn(' ', Adventure_Name+'.DA5');
        Assign(CommandFile, Adventure_Name+'.DA5');
        Rewrite(CommandFile);
        FOR i := 1 TO Last_CMD DO Write(CommandFile, SpecialCMD[i]^);
        Close(CommandFile);
      END;

    WriteLn;
    WriteLn('And optionally these files:');
    WriteLn(' ', Adventure_Name+'.TTL -- a title file');
    WriteLn(' ', Adventure_Name+'.INS -- an instruction file');
    WriteLn;
    Halt;
  END;                            {WriteFinalVersion}

